<?php
declare(strict_types=1);

namespace HiCommon\Service;

use HiCommon\Repository\OrderRepairstoreRepository;
use HiCommon\Repository\OrderMallRepository;
use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\OrderCheckpointVehicleRepository;
use HiCommon\Repository\StoreOrderRepository;

class StatisticsService extends BaseService
{
    /**
     * 门店数据统计
     */
    public function statistics($n_shop_id,$data)
    {
       $arr_data = [];

        //图线表数据
        $res = ShopService::api_graphs_statistics($n_shop_id,$data);
        if($res['state'] != 0){
            return $res;
        }
        $arr_data['line'] = $res['data'];

        //柱状图数据
        $data['date_type'] = 'half_year';
        $res = ShopService::api_graphs_statistics($n_shop_id,$data);
        if($res['state'] != 0){
            return $res;
        }
        $arr_data['graphs'] = $res['data'];

        return  hello_success('成功',$arr_data);

    }

    /**
     * 检测站（预约订单统计）
     */
    public function checkpointOrderNum($data)
    {
        $array = self::dateArray($data['time_cate']);
        $order = OrderCheckpointRepository::get_all_by_where(['shop_id' => $data['shop_id']], ['id']);

        $data['order_ids'] = [];
        foreach ($order as $or => $order) {
            $data['order_ids'][] = $order['id'];
        }
        $list = OrderCheckpointVehicleRepository::count_by_where((array)$data, (array)$array);

        $acting = array();
        $price = [];
        foreach ($list[1] as $d => $day) { //代驾
            $acting[$day['date']] = $day['total'];
            $price[$day['date']] = $day['price'];
        }
        $dai = array();
        foreach ($array as $key => $value) {//代驾
            if (array_key_exists($value, $acting)) {
                $dai['num'][$key] = $acting[$value];
                $dai['price'][$key] = $price[$value];
            } else {
                $dai['num'][$key] = 0;
                $dai['price'][$key] = 0;
            }
            $dai['date'][$key] = $value;
        }

        $detail = array();
        $price2 = [];
        foreach ($list[2] as $d => $day) { //自驾
            $detail[$day['date']] = $day['total'];
            $price2[$day['date']] = $day['price'];
        }

        $zi = array();//自驾
        foreach ($array as $key => $value) {
            if (array_key_exists($value, $detail)) {
                $zi['num'][$key] = $detail[$value];
                $zi['price'][$key] = $price2[$value];
            } else {
                $zi['num'][$key] = 0;
                $zi['price'][$key] = 0;
            }
            $zi['date'][$key] = $value;
        }

        return ['zi' => $zi, 'dai' => $dai, 'total' => $list[3]];
    }

    /**
     * 维修店(商品订单)数据统计
     */
    public function goodsOrderNum($data)
    {
        $array = self::dateArray($data['time_cate']);
        /* //积分订单统计
         $list = OrderMallRepository::count_by_where((int)$data['shop_id'], (int)$data['time_cate'], (array)$array);
         $detail = array();
         foreach ($list[1] as $d => $day) {
             $detail[$day['date']] = $day['total'];
             $integral[$day['date']] = $day['integral'];
         }

         $census = array();
         foreach ($array as $key => $value) {
             if (array_key_exists($value, $detail)) {
                 $census['num'][$key] = $detail[$value];
                 $census['integral'][$key] = (int)$integral[$value];
             } else {
                 $census['num'][$key] = 0;
                 $census['integral'][$key] = 0;
             }
             $census['date'][$key] = $value;
         }*/
        //普通商品订单统计
        $list = StoreOrderRepository::count_by_where((int)$data['shop_id'], (int)$data['time_cate'], (array)$array);
        $detail = array();
        $amount = [];
        foreach ($list[1] as $d => $day) {
            $detail[$day['date']] = $day['total'];
            $amount[$day['date']] = $day['amount'];
        }

        $census = array();
        foreach ($array as $key => $value) {
            if (array_key_exists($value, $detail)) {
                $census['num'][$key] = $detail[$value];
                $census['amount'][$key] = (int)$amount[$value];
            } else {
                $census['num'][$key] = 0;
                $census['amount'][$key] = 0;
            }
            $census['date'][$key] = $value;
        }

        return ['census' => $census, 'total' => $list[3]];
    }

    /**
     * 维修店(服务订单)数据统计
     */
    public function repairOrderNum($data)
    {
        $array = self::dateArray($data['time_cate']);

        if ($data['time_cate'] == 2) {
            $list = OrderRepairstoreRepository::count_by_where_month((int)$data['shop_id'], (array)$array);
        } else {
            $list = OrderRepairstoreRepository::count_by_where_days((int)$data['shop_id'], (array)$array);
        }

        $detail = array();
        $amount = [];
        foreach ($list[1] as $d => $day) { //自驾
            $detail[$day['date']] = $day['total'];
            $amount[$day['date']] = $day['amount'];
        }

        $zi = array();//自驾
        foreach ($array as $key => $value) {
            if (array_key_exists($value, $detail)) {
                $zi['num'][$key] = $detail[$value];
                $zi['amount'][$key] = (float)$amount[$value];
            } else {
                $zi['num'][$key] = 0;
                $zi['amount'][$key] = 0;
            }
            $zi['date'][$key] = $value;
        }

        $acting = array();
        $acamount = [];
        foreach ($list[2] as $d => $day) { //代驾
            $acting[$day['date']] = $day['total'];
            $acamount[$day['date']] = $day['amount'];
        }
        $dai = array();
        foreach ($array as $key => $value) {//代驾
            if (array_key_exists($value, $acting)) {
                $dai['num'][$key] = $acting[$value];
                $dai['amount'][$key] = (float)$acamount[$value];
            } else {
                $dai['num'][$key] = 0;
                $dai['amount'][$key] = 0;
            }
            $dai['date'][$key] = $value;
        }

        return ['zi' => $zi, 'dai' => $dai, 'total' => $list[3]];
    }

    /**
     * 日期计算
     */
    public function dateArray($cate)
    {
        $time = time();
        if ($cate == 1) {
            for ($i = 0; $i <= 6; $i++) {
                $date[$i] = date('Y-m-d', strtotime('+' . $i - 6 . 'days', $time));
            }
        } elseif ($cate == 2) {
            for ($m = 0; $m <= 5; $m++) {
                $date[$m] = date('Y-m', strtotime('+' . $m - 5 . 'month', $time));
            }
        } else {
            $month = date('Y-m', $time);
            $allday = date('d', strtotime("$month +1 month -1 day"));

            for ($d = 1; $d <= $allday; $d++) {
                $day = $d > 9 ? $d : '0' . $d;
                $date[] = $month . '-' . $day;
            }
        }

        return $date ?? [];
    }

}